<template>
	<view class="content">
		<uni-nav-bar
			@clickLeft="back"
			left-icon=""
			:status-bar="true"
			:border="true"
			:shadow="false"
			:fixed="true">
			<view slot="left">
				<text class="back xyicon xyicon-left"></text>
			</view>
			<uni-search-bar
				@input="searchChange"
				@confirm="loadData('refresh')"
				@cancel="searchCancal"
				:placeholder="keyword?keyword:'请输入关键字'"
				radius="20"
				bgColor="rgba(231, 231, 231,.7)"/>
		</uni-nav-bar>
		<view class="navbar" :style="{top: statusBarHeight + 44 + 'px'}">
			<view class="nav-item" :class="{current: orderby === ''}" @click="tabClick('')">
				综合排序
			</view>
			<view class="nav-item" :class="{current: orderby === 'buyCount-desc'}" @click="tabClick('buyCount-desc')">
				销量优先
			</view>
			<view class="nav-item" :class="{current: orderby === 'price'}" @click="tabClick('price')">
				<text>价格</text>
				<view class="p-box">
					<text :class="{active: priceOrder === 'asc' && orderby === 'price'}" class="xyicon xyicon-up"></text>
					<text :class="{active: priceOrder === 'desc' && orderby === 'price'}" class="xyicon xyicon-down"></text>
				</view>
			</view>
			<text class="cate-item filter-btn xyicon xyicon-filter-o" @click="toggleCateMask('show')"></text>
		</view>
		<view :scroll-y="true" :enable-flex="true" class="goods-list">
			<view
				v-for="(item, index) in itemList" :key="index"
				class="goods-item"
				@click="navToDetailPage(item)">
				<view class="image-wrapper">
					<xy-image :src="item.cover" mode="aspectFill"></xy-image>
				</view>
				<text class="title clamp">{{item.title}}</text>
				<view class="price-box">
					<text class="price">{{item.price}}</text>
					<text>已售 {{item.buyCount}}</text>
				</view>
			</view>
		</view>
		<uni-load-more v-if="itemList.length > 0" :status="loadingType"></uni-load-more>
		<view v-if="itemList.length == 0" class="m-t text-gray" style="text-align: center;">暂无商品</view>
		<view class="cate-mask" :class="cateMaskState===0 ? 'none' : cateMaskState===1 ? 'show' : ''" @click="toggleCateMask">
			<scroll-view scroll-y class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
				<view class="box">
					<view class="title">
						价格区间
					</view>
					<view class="price-range">
						<input type="number" v-model="priceRange.start" />
						<view class="hg">-</view>
						<input type="number" v-model="priceRange.stop" />
					</view>
				</view>
				<view class="box">
					<view class="title">
						分类
					</view>
					<view class="cate-list">
						<view v-for="item in icateList" :key="item.id" v-if="item.level == 1">
							<view class="cate-item b-b two">{{item.title}}</view>
							<view 
								v-if="tItem.pid == item.id"
								v-for="tItem in icateList" :key="tItem.id" 
								class="cate-item cate-item-children b-b" 
								:class="{active: tItem.id == cateId}"
								@click="changeCate(tItem)">
								{{tItem.title}}
							</view>
						</view>
					</view>
				</view>
				<view class="box">
					<view class="action">
						<button @click="submit" class="submit" type="primary">筛选</button>
						<button @click="cancel" class="cancel" type="default">重置</button>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	var statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
	export default {
		data() {
			return {
				statusBarHeight: statusBarHeight,
				cateMaskState: 0, //分类面板展开状态
				loadingType: 'more', //加载更多状态
				page: 1,
				orderby: '', 
				icateId: 0, // 分类ID，保存旧值用
				cateId: 0, //分类id
				priceOrder: 'asc',
				cateList: [],
				itemList: [],
				icateList: [],
				priceRange: {
					start: '',
					stop: ''
				},
				keyword: ''
			};
		},
		onLoad(options){
			this.icateId = options.icateId; //保存用来重置用
			this.cateId = this.icateId;
			if (options.keyword) {
				this.keyword = options.keyword;
			}
			this.loadData();
		},
		// 加载更多
		onReachBottom(){
			this.page = this.page + 1;
			this.loadData();
		},
		async onPullDownRefresh() {
			await this.loadData('refresh');
			uni.stopPullDownRefresh();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 5000);
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {// 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: this.$store.state.app.siteInfo.title,
				path: '/pages/ishop/item/lists'
			}
		},
		methods: {
			back(){
				// 获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
				var pages = getCurrentPages();
				if (pages.length == 1) {
					uni.switchTab({
						url: '/pages/home'
					})
				} else {
					var prePage = pages[pages.length - 2];
					if (prePage.$vm && prePage.$vm.setKeyword) {
						prePage.$vm.setKeyword(this.keyword);
					}
					setTimeout(()=>{
						uni.navigateBack();
					}, 800);
				}
			},
			searchChange(e) {
				this.keyword = e.value;
			},
			searchCancal() {
				this.keyword = '';
				this.loadData('refresh');
			},
			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type='add', loading) {
				// 没有更多直接返回
				if(type === 'add'){
					if(this.loadingType === 'nomore'){
						return;
					}
					this.loadingType = 'loading';
				}else{
					this.loadingType = 'more'
				}
				
				// 请求接口
				let orderby = this.orderby;
				if (orderby == 'price') {
					orderby = orderby + '-' + this.priceOrder;
				}
				let data = {
					icateId: this.cateId,
					order: orderby,
					page: this.page,
					keyword: this.keyword
				}
				if (this.priceRange.start > this.priceRange.stop) {
					this.util.showToast({
						title: '价格筛选错误',
						icon: 'loading',
						duration: 2000
					});
				}
				if (this.priceRange.stop > 0) {
					data.price = this.priceRange.start + ',' + this.priceRange.stop;
				}
				let res = await this.util.request({
					url: '/v1/ishop/item/lists',
					method: 'get',
					data: data
				});
				if (res.code != 200) {
					this.util.showToast({
						title: res.msg,
						icon: 'loading',
						duration: 2000
					});
				}
				if(type === 'refresh'){
					this.itemList = [];
				}
				// 筛选
				this.itemList = this.itemList.concat(res.data.dataList);
				if (this.icateId == this.cateId) {
					this.icateList = res.data.icateList;
				}
				//判断是否还有下一页，有是more没有是nomore
				this.loadingType  = this.itemList.length > res.data.dataPage.totalCount ? 'nomore' : 'more';
				if(type === 'refresh'){
					if(loading == 1){
						uni.hideLoading()
					}else{
						uni.stopPullDownRefresh();
					}
				}
			},
			//筛选点击
			tabClick(index){
				if(this.orderby === index && index !== 'price'){
					return;
				}
				this.page = 1;
				this.orderby = index;
				if(index === 'price'){
					this.priceOrder = this.priceOrder === 'asc' ? 'desc' : 'asc';
				} else {
					this.priceOrder = '';
				}
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
			},
			//显示分类面板
			toggleCateMask(type){
				let timer = type === 'show' ? 10 : 300;
				let	state = type === 'show' ? 1 : 0;
				this.cateMaskState = 2;
				setTimeout(()=>{
					this.cateMaskState = state;
				}, timer)
			},
			// 分类点击
			changeCate(item){
				this.cateId = item.id;
			},
			// 筛选
			submit(){
				this.page = 1;
				this.toggleCateMask();
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				});
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				});
			},
			// 取消筛选
			cancel() {
				this.page = 1;
				this.cateId = this.icateId;
				this.priceRange = {
					start: '',
					stop: ''
				};
				this.toggleCateMask();
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				});
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				});
			},
			//详情
			navToDetailPage(item){
				let id = item.id;
				uni.navigateTo({
					url: `/pages/ishop/item/info?id=${id}`
				})
			},
			stopPrevent(){}
		},
	}
</script>

<style lang="scss" scoped>
	.navbar{
		position: sticky;
		left: 0;
		display: flex;
		width: 100%;
		height: 40px;
		background: #fff;
		border-top: 1px solid #ccc;
		box-shadow: 0 2upx 5px rgba(0,0,0,.06);
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30upx;
			color: $font-color-dark;
			position: relative;
			&.current{
				color: $uni-color-primary;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 120upx;
					height: 0;
					border-bottom: 4upx solid $uni-color-primary;
				}
			}
		}
		.p-box{
			display: flex;
			flex-direction: column;
			.xyicon{
				font-weight: 700;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 14upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #777;
				&.active{
					color: $uni-color-primary;
				}
			}
			.xia{
				transform: scaleY(-1);
			}
		}
		.cate-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 44upx;
			&:after{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}
	.filter-btn {
		color: $font-color-dark;
		font-size: 18px !important;
	}

	/* 商品列表 */
	.goods-list{
		display:flex;
		flex-wrap:wrap;
		margin-top: 0px;
		padding: 15px;
		// height: calc(100vh - 150px);
		background: #fff;
		.goods-item{
			display:flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;
			&:nth-child(2n+1){
				margin-right: 4%;
			}
		}
		.image-wrapper{
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;
			image{
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}
		.title{
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}
		.price-box{
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $font-color-light;
		}
		.price{
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
			&:before{
				content: '￥';
				font-size: 26upx;
			}
		}
	}
	
	/* 侧边筛选 */
	.cate-mask{
		position: fixed;
		left: 0;
		top: calc(44px + env(safe-area-inset-top));
		bottom: 0;
		width: 100%;
		background: rgba(0,0,0,0);
		z-index: 95;
		transition: .3s;
		.cate-content{
			width: 500upx;
			height: 100%;
			background: #fff;
			float:right;
			transform: translateX(100%);
			transition: .3s;
			.box {
				.title {
					font-size: 15px;
					padding: 15px 15px 5px;
				}
				.price-range {
					padding: 0px 15px;
					display: flex;
					.hg {
						padding: 0px 5px;
					}
					input {
						width: 180upx;
						border: 1px solid #ddd;
						padding: 2px 5px;
						border-radius: 4px;
					}
				}
				.cate-list{
					display: flex;
					flex-direction: column;
					height: 100%;
					.cate-item{
						display: flex;
						align-items: center;
						height: 90upx;
						padding-left: 30upx;
						font-size: 28upx;
						color: #555;
						position: relative;
					}
					.cate-item-children {
						padding-top: 5px;
						height: 35px;
						display: inline-block;
					}
					.two{
						height: 64upx;
						color: #303133;
						font-size: 30upx;
						background: #f8f8f8;
					}
					.active{
						color: $uni-color-primary;
					}
				}
				.action {
					padding: 20px;
					display: flex;
					.submit {
						background-color: $uni-color-primary;
					}
				}
			}
		}
		&.none{
			display: none;
		}
		&.show{
			background: rgba(0,0,0,.4);
			
			.cate-content{
				transform: translateX(0);
			}
		}
	}
</style>
